﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualTool_Weixiu : Form
    {
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string str, 項次, 手工具編號, 手工具名稱, 領用日期, 受理人, 歸還日期, 領用數量;
        bool b手工具Search, b手工具Insert, b手工具Edit, b手工具Del;
        public ManualTool_Weixiu(string str, bool b手工具Search, bool b手工具Insert, bool b手工具Edit, bool b手工具Del)
        {
            InitializeComponent();
            this.str = str;
            this.b手工具Search = b手工具Search;
            this.b手工具Insert = b手工具Insert;
            this.b手工具Edit = b手工具Edit;
            this.b手工具Del = b手工具Del;
        }

        private void bt_搜尋_Click(object sender, EventArgs e)
        {
            if (!b手工具Search)
            { 
                MessageBox.Show("很抱歉，您沒有權限！","錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (str.Equals("維修"))
                {
                    search("sp_手工其維修_search");
                }
                if (str.Equals("送檢"))
                {
                    search("sp_手工具送檢_search");
                }
                if (str.Equals("報廢"))
                {
                    search("sp_手工具報廢_search");
                }
            }           
        }

        public void search(string sp)
        {
            try
            {
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                //string sp = "sp_手工其維修_search";
                SqlCommand cmd0 = new SqlCommand(sp, con);
                cmd0.CommandType = CommandType.StoredProcedure;
                //if (check_領用日期.Checked.Equals(true))
                //{
                    cmd0.Parameters.AddWithValue("@領用日期_1", dateTime_領用日期_1.Value);
                    cmd0.Parameters.AddWithValue("@領用日期_2", dateTime_領用日期_2.Value);
                //}
                //if (check_歸還日期.Checked.Equals(true))
                //{
                    cmd0.Parameters.AddWithValue("@歸還日期_1", dateTime_歸還日期_1.Value);
                    cmd0.Parameters.AddWithValue("@歸還日期_2", dateTime_歸還日期_2.Value);
                //}
                if (txt_手工具名稱.Text != "") cmd0.Parameters.AddWithValue("@手工具名稱", txt_手工具名稱.Text.ToString());
                if (txt_手工具編號.Text != "") cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                if (txt_受理人.Text != "") cmd0.Parameters.AddWithValue("@受理人", txt_受理人.Text.ToString());

                SqlDataReader sqlreader = cmd0.ExecuteReader();
                DataTable dt = new DataTable();
                //手工具領用.項次,手工具.手工具編號,手工具.手工具名稱, 手工具.規格型號,手工具領用部門.部門名稱 AS 領用部門, 
                //手工具領用人.姓名 AS 領用人, 手工具受理人.姓名 AS 受理人, 手工具領用.數量,手工具領用.領用日期,手工具領用.歸還日期, 手工具領用.備註

                dt.Columns.Add("項次");
                dt.Columns.Add("手工具編號");
                dt.Columns.Add("手工具名稱");
                dt.Columns.Add("規格型號");
                dt.Columns.Add("受理人");
                dt.Columns.Add("數量");
                dt.Columns.Add(str+"日期");
                dt.Columns.Add("預定歸還日期");
                dt.Columns.Add("備註");

                while (sqlreader.Read())
                {
                    DataRow dtrows = dt.NewRow();
                    dtrows["項次"] = sqlreader["項次"];
                    dtrows["手工具編號"] = sqlreader["手工具編號"];
                    dtrows["手工具名稱"] = sqlreader["手工具名稱"];
                    dtrows["規格型號"] = sqlreader["規格型號"];
                    dtrows["受理人"] = sqlreader["受理人"];
                    dtrows["數量"] = sqlreader["數量"];
                    dtrows[str+"日期"] = sqlreader[str+"日期"];
                    dtrows["預定歸還日期"] = sqlreader["預定歸還日期"];
                    dtrows["備註"] = sqlreader["備註"];
                    dt.Rows.Add(dtrows);
                }

                dataGrid_領用.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_領用);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //con.Close();
            }
        }

        private void bt_歸還_Click(object sender, EventArgs e)
        {
            ManualTool_Weixiu_Return frm = new ManualTool_Weixiu_Return(str,項次, 手工具編號, 手工具名稱, 領用日期, 受理人, 歸還日期, 領用數量);
            frm.ShowDialog();
            if (str.Equals("維修"))
            {
                Load_手工其維修("sp_手工其維修_load");
            }
            if (str.Equals("送檢"))
            {
                Load_手工其維修("sp_手工具送檢_load");
            }
            if (str.Equals("報廢"))
            {
                Load_手工其維修("sp_手工具報廢_load");
            }
        }
        public void Load_手工其維修(string procedure)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(procedure, con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGrid_領用.DataSource = dt;
                PublicClass.setRowNumber(dataGrid_領用);
                dataGrid_領用.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }

        private void dataGrid_領用_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //手工其維修.項次,手工具.手工具編號,手工具.手工具名稱, 手工具.規格型號, 手工具受理人.姓名 AS 受理人
            //手工其維修.數量,手工其維修.維修日期,手工其維修.預定歸還日期, 手工其維修.備註
            DataGridViewRow row = new DataGridViewRow();
            row = dataGrid_領用.Rows[e.RowIndex];
            項次 = row.Cells[0].Value.ToString();
            手工具編號 = row.Cells[1].Value.ToString();
            手工具名稱 = row.Cells[2].Value.ToString();

            受理人 = row.Cells[4].Value.ToString();
            領用數量 = row.Cells[5].Value.ToString();
            領用日期 = row.Cells[6].Value.ToString();
            歸還日期 = row.Cells[7].Value.ToString();
        }

        private void ManualTool_Weixiu_Load(object sender, EventArgs e)
        {
            if (str.Equals("維修"))
            {
                this.Text = "手工具維修";
                lb_日期.Text = "維修日期";
                Load_手工其維修("sp_手工其維修_load");
            }
            if (str.Equals("送檢"))
            {
                this.Text = "手工具送檢";
                lb_日期.Text = "送檢日期";
                Load_手工其維修("sp_手工具送檢_load");
            }
            if (str.Equals("報廢"))
            {
                this.Text = "手工具報廢";
                lb_日期.Text = "報廢日期";
                Load_手工其維修("sp_手工具報廢_load");
            }
            if (!b手工具Edit)
            {
                bt_歸還.Enabled = false;
            }
            if (!b手工具Search)
            {
                bt_搜尋.Enabled = false;
            }
            if (!b手工具Insert)
            {
                bt_歸還.Enabled = false;
            }
        }

        private void bt_重置_Click(object sender, EventArgs e)
        {
            txt_受理人.Text = "";
            txt_手工具名稱.Text = "";
            txt_手工具編號.Text = "";
            dateTime_歸還日期_1.Value = dateTime_歸還日期_1.MinDate;
            dateTime_歸還日期_2.Value = dateTime_歸還日期_2.MaxDate;
            dateTime_領用日期_1.Value = dateTime_領用日期_1.MinDate;
            dateTime_領用日期_2.Value = dateTime_領用日期_2.MaxDate;
            if (str.Equals("維修"))
            {
                Load_手工其維修("sp_手工其維修_load");
            }
            if (str.Equals("送檢"))
            {
                Load_手工其維修("sp_手工具送檢_load");
            }
            if (str.Equals("報廢"))
            {
                Load_手工其維修("sp_手工具報廢_load");
            }
        }
    }
}
